#!/usr/bin/env python
#coding: utf-8
'''
Created on 2017年4月17日

@author: Administrator
'''
from django.contrib.auth.models import User
from django.contrib.contenttypes import generic
from django.contrib.contenttypes.models import ContentType
from django.db import models

from comment import settings
from common.models import TimeModel, StateModel
from common.models.abstract import LikeModel


class CommentModel(models.Model):
    '''
    class: 所有使用了Comment的都必须继承次父类
    '''
    
    comment_count = models.IntegerField(default=0, verbose_name=u'评论数')
    comment_time = models.DateTimeField(auto_now_add=True, db_index=True, verbose_name=u'最后评论时间')
    comment_able = models.SmallIntegerField(default=settings.BoolNumberEnum.YES, choices=settings.BOOL_NUMBER_CHOICES, verbose_name=u'是否可评论')
    
    class Meta:
        abstract = True
        

class Comment(TimeModel, StateModel, LikeModel):
    """
    Holds the relationship between a tag and the item being tagged.
    """
    user = models.ForeignKey(User, verbose_name=u'评论者')
    
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField(db_index=True)
    object = generic.GenericForeignKey('content_type', 'object_id')

    parent = models.ForeignKey('Comment', null=True, blank=True, related_name='comment_parent', verbose_name=u'父级评论')
    content = models.TextField(default='', verbose_name=u'内容')
    text_content = models.TextField(default='', blank=True, verbose_name=u'纯文本内容')
    
    floor = models.IntegerField(default=0, verbose_name=u'楼层')
    source = models.IntegerField(default=0, choices=settings.SOURCE_CHOICES, db_index=True, verbose_name=u'来源')
    source_id = models.BigIntegerField(default=0, verbose_name="来源id")
    
    class Meta:
        app_label = 'comment'
        verbose_name = u'评论'
        verbose_name_plural = u'评论'
        unique_together = ("source", "source_id")

    @property
    def children(self):
        '''子评论'''
        return Comment.objects.filter(parent_id=self.id)
